<?php
namespace app\admin\controller;

class Message extends Admin {
    public function __construct() {
        parent::__construct();
        $this->model = "Message";
        $this->assign("model", $this->model);
	}
	
	public function index() {
		$getData = input('param.');
		$whereData = '';
		if ($getData) {
			foreach ($getData as $k => $v) {
				if (!$whereData) {
					$whereData .= $k . '=' . $v;
				} else {
					$whereData .= '&' . $k . '=' . $v;
				}
			}
		}
		$taglist = [];
		$senduserlist = [];
		if ($this->uinfo['roleid'] == '6') {
			$taglist = model('Tag')->where('tagdealer', $this->uinfo['dealerid'])->column('id, tagname');
		} else {
			$taglist = model('Tag')->column('id, tagname');
			if ($this->uinfo['roleid'] == '1' || $this->uinfo['roleid'] == '4'){
				$senduserlist = model('AdminUser')->whereOr('roleid', '1')->whereOr('roleid', '4')->whereOr('roleid', '6')->column('userid, realname');
			}
		}
		$uinfo = $this->uinfo;
		$this->assign('keywords', isset($getData['keywords']) ? $getData['keywords'] : '');
		$this->assign("whereData", $whereData);
		$this->assign("uinfo", $uinfo);
		$this->assign("taglist", $taglist);
		$this->assign("senduserlist", $senduserlist);
		return $this->fetch();
	}

	// 打开添加消息的界面
	public function add() {
		$where = [];
		if($this->uinfo['roleid'] == '6') {
			$where['tagdealer'] = $this->uinfo['dealerid'];
		}
		$taglist = model('Tag')->where($where)->select();
		$this->assign('taglist', $taglist);
		return $this->fetch();
	}

	// 保存新添加的消息
	public function save_message_data() {
		$postData = input('post.postData');
		parse_str($postData, $dataarr);
		$data = [
			'title' => $dataarr['title'],
			'content' => $dataarr['content'],
			'status' => 1,
			'addtime' => time(),
			'send_user' => $this->uinfo['userid'],
		];
		$id = model('Message')->save_data($data);
		$tagids = $dataarr['tagid'];
		// 将tagid中的所有用户，unread改为1，说明有新消息
		$userids = model('UserTag')->whereIn('tagid', $tagids)->column('userid');
		if ($userids) {
			$count = model('AdminUser')->whereIn('userid', $userids)->setField('unread', 1);
		}
		// 将消息跟标签进项关联
		$tmdata = [];
		foreach ($tagids as $value) {
			$itemdata = [
				'messageid' => $id,
				'tagid' => $value,
			];
			$tmdata[] = $itemdata;
		}
		$result = model('TagMessage')->saveAll($tmdata);
		if ($result === false) {
			return json(['status' => 0, 'msg' => model('Message')->getError()]);
		}
		return json(['status' => 1, 'msg' => '操作成功']);
	}

	// 删除消息
	public function data_delete() {
		$items = input("param.items");
		$items = trim($items, ",");
		$model = input("param.model");
		trace($items);
		trace($model);
		$message_ids = model('TagMessage')->whereIn('id', $items)->column('messageid');
		if ($message_ids) {
			model('Message')->whereIn('id', $message_ids)->delete();
			model('TagMessage')->whereIn('id', $items)->delete();
		}
		return json(['status' => 1, 'msg' => '操作成功']);
		// $db = model($model);
		// $db->delete_data(['item_ids' => $items]);

		// return json(['status' => 1, 'msg' => '操作成功']);
	}

    public function send_sysmsg(){
    	$info = model('Member')->get_data(['item_id'=>1]);
    	$where['id'] = ['NEQ', 1];
    	$ulist = model("Member")->list_data($where);
    	$data['userid'] = $info['id'];
    	$data['name'] = $info['realname'];
    	$data['avatar'] = urlencode($info['avatar']);
    	$data['typeid'] = 4;
    	$touser = [];
    	foreach ($ulist as $v) {
    		$data['touserid'] = $v['id'];
    		$ids = model("ChatRecord")->save_data($data);
    		if ($ids) {
    			$touser[] = $v['id'];
    		}
    	}
    	$data['touserid'] = $touser;
    	$datas = send_sysmsg($data);
    	if ($datas['code'] == 200) {
    		return json(['status' => 1,'msg'=>'操作成功']);
    	}else{
    		return json(['status' => 0,'msg'=>'操作失败']);
    	}
	}
	
	// 获取当前账号发布的消息列表
	public function get_message_list() {
		$tagid = input('param.tagid');
		$senduserid = input('param.senduserid');
		$keywords = input('param.keywords');
		$whereDatas = [];
		// if ($this->uinfo['roleid'] == '6') {
		// 	$whereDatas['send_user'] = $this->uinfo['dealerid'];
		// }
		// $whereTag = [];
		// if ($tagid) {
		// 	$whereTag = model('TagMessage')->where('tagid', $tagid)->column('messageid');
		// }
		if ($senduserid) {
			$whereDatas['send_user'] = $senduserid;
		}
 		if($keywords) {
			$whereDatas['title|content'] = ['like', "%$keywords%"];
		}
		$order = input("param.order");
		if ($this->uinfo['roleid'] == '6') {
			$whereDatas['send_user'] = $this->uinfo['dealerid'];
		}
		$messageids = model('Message')->where($whereDatas)->column('id');
		$messagelist = [];
		if ($messageids) {
			// $messagelist = model('TagMessage')->where()->select();
			if ($tagid) {
				list($messagelist, $where) = model('TagMessage')->admin_get_list(0, 0, ['messageid' => ['in', $messageids], 'tagid' => $tagid], $order);
			} else {
				list($messagelist, $where) = model('TagMessage')->admin_get_list(0, 0, ['messageid' => ['in', $messageids]], $order);
			}
		}
		$count = count($messagelist);
		$return = [
			'code' => 0,
			'msg' => '',
			'count' => $count,
			'data' => $messagelist,
		];
		trace($messagelist);
		return json($return);
	}
	
}